package com.example.perplonews.a_d_fragment.e_mine

import BaseActivity
import android.content.Intent
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.example.perplonews.R
import com.example.perplonews.a_a_mvp.contract.Login_Contract
import com.example.perplonews.a_a_mvp.model.HttpCode
import com.example.perplonews.a_a_mvp.model.models.Login_Model
import com.example.perplonews.a_a_mvp.presenter.presenters.Login_Presenter
import com.example.perplonews.a_b_entity.BaseEntity
import com.example.perplonews.a_b_entity.ResponseUserEntity
import com.example.perplonews.a_b_entity.greendao.UserEntity
import com.example.perplonews.a_b_utils.App
import com.example.perplonews.a_b_utils.LjlUtils
import kotlinx.android.synthetic.main.view_user_edt.*

class Login_Activity : BaseActivity<Login_Presenter>(), Login_Contract.LoginView {
    private val headBackImg: ImageView by lazy { findViewById<ImageView>(R.id.head_back_img) }
    private val loginPhoneEdt: EditText by lazy { findViewById<EditText>(R.id.login_phone_edt) }
    private val loginShowImg: ImageView by lazy { findViewById<ImageView>(R.id.login_show_img) }
    private val loginPwdEdt: EditText by lazy { findViewById<EditText>(R.id.login_pwd_edt) }
    private val loginBtn: Button by lazy { findViewById<Button>(R.id.login_btn) }
    private val loginRegisterTv: TextView by lazy { findViewById<TextView>(R.id.login_register_tv) }

    override fun findlayout(): Int {
        return R.layout.activity_login
    }

    override fun initView() {

    }

    override fun initData() {
        mypre = Login_Presenter(Login_Model(), this)
        loginBtn.setOnClickListener {

            mypre.requestLogin(
                java.lang.Long.valueOf(login_phone_edt.getText().toString()),
                login_pwd_edt.getText().toString(),
                HttpCode.LOGINCODE
            )

        }
        loginRegisterTv.setOnClickListener {
            startActivity(Intent(this, RegisterActivity::class.java))
            finish()
        }
    }


    override fun refreshView(entity: BaseEntity) {
        if (entity is ResponseUserEntity) {

            var user = entity as ResponseUserEntity
            if (user.status.equals("200")) {
                var values = user.getValues()

                //发送广播  来更改页面
                var intent = Intent()
                intent.setAction("islog")
                intent.putExtra("userdata",values)
                sendBroadcast(intent)



                if (values != null) {
                    //存入数据库
                    App.getDaoSession()?.deleteAll(UserEntity::class.java)
                    App.getDaoSession()?.insert(values)
                    finish()
                }

            } else {

                LjlUtils.Ljl_Toast(this, "" + user.message)
            }


        }

    }

}